package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.model.behavior.dtos.UnLikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.exception.CustException;
import com.heima.model.threadLocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.DeleteResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApUnlikeBehaviorServiceImpl implements ApUnlikeBehaviorService {
    @Autowired
    private ApBehaviorEntryServiceImpl apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult unlikeBehavior(UnLikesBehaviorDTO dto) {
        // 1 校验参数 文章id不能为空 不喜欢类型取值为 0 或 1 利用注解开发
        // 需要登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "当前用户未登录!");
        }
        // 2 查询行为实体数据
        Integer id = user.getId();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(id, null);
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "行为实体对象不存在或者创建失败!");
        }
        //3. 如果是 不喜欢操作 查询不喜欢行为是否存在
        if (ApUnlikesBehavior.Type.UNLIKE.getCode() == dto.getType()) {
            Query query = Query.query(Criteria.where("type").is(dto.getType()).and("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            List<ApUnlikesBehavior> apUnlikesBehaviorList = mongoTemplate.find(query, ApUnlikesBehavior.class);
            //4. 如果不存在   添加不喜欢行为
            if (CollectionUtils.isNotEmpty(apUnlikesBehaviorList)) {
                CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"已经添加为不喜欢!");
            }
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setEntryId(apBehaviorEntry.getId());
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setType(ApUnlikesBehavior.Type.UNLIKE.getCode());
            ApUnlikesBehavior save = mongoTemplate.save(apUnlikesBehavior);
            System.out.println(save);
            return ResponseResult.okResult("新增不喜欢行为成功");
        } else {
            //5. 如果是 取消不喜欢操作  删除对应的不喜欢数据
            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            DeleteResult remove = mongoTemplate.remove(query, ApUnlikesBehavior.class);
            System.out.println(remove);
            return ResponseResult.okResult("取消不喜欢操作成功!");
        }
    }
}
